#ifndef _CAN_H_
#define _CAN_H_


#include "sn_config.h"
#define CAN_LIST_MAX 2
#define CAN_MOD_ON_COUNT 3//模块在位计数值
typedef enum { 
    CAN_MSG_OK=0x00,
    CAN_MSG_CMDNUM_ERR=0x02,
    CAN_MSG_DATA_ERR=0x03,
    CAN_MSG_ADDR_ERR=0x04,
    CAN_MSG_RUN_ERR=0x07
}can_msg_error_t;

typedef enum {
    CAN_MSG_VER_A=0xA,
    CAN_MSG_VER_B=0xB,
}can_msg_ver_t;
struct can_msg_ext_id_t 
{
    can_msg_error_t err ;
    can_msg_ver_t  ver;
    uint8_t cmd;
    uint8_t  master_addr;
    uint32_t slave_addr;
};

struct can_obj_t
{
    uint32_t slave_addr;//源地址 本机地址
    uint8_t master_addr;//目标地址，远端地址
	  uint32_t rx_id;
    uint32_t tx_id;
    uint8_t err_num;
    uint8_t dev_num;
    uint8_t cmd_num;
		uint8_t ide;
		uint8_t rtr;
		uint8_t TX[8];
		uint8_t RX[8];
		uint8_t rx_sta;
};
struct can_list_t {
    uint32_t addr;
    uint8_t power;
    uint8_t  ave_flag; //均流标志
    uint16_t ave_i_val;//均流值
    /*ac val*/
    uint32_t ac_ab_val;
    uint32_t ac_bc_val;
    uint32_t ac_ca_val;
    uint32_t sys_vout ; //系统电压
    uint32_t sys_iout ; //系统电流

    uint32_t mod_vout; //模块电压 
    uint32_t mod_iout;//模块电流

    uint32_t mod_vo_max ;//模块电压最大值
    uint32_t mod_vo_min;//模块电压最小值
    uint32_t mod_io_max;//模块电压最大值
    uint8_t mod_state[4];//模块状态 cmd4 温度，状态2 状态1 状态0 
    int on_cnt ;//在位计数 on_cnt = 0 clear on
    uint8_t  on;//在位状态
    //struct can_list_t *next;
};
 
struct can_dev_t
{
    uint8_t initflag;
    uint8_t rx_flag;//接收标志
    uint8_t tx_flag;//发送标志
    uint8_t running_flag;//运行标志
    struct can_sys_master_t  *sys_master;
    struct can_list_t can_dev_list[CAN_LIST_MAX];
	struct can_obj_t obj;
    uint8_t limit_flag ;//限制标志 bit0 均流，bit1 限压
    uint16_t vout_limit;//电压限定值
    /**
     * @brief 接收完成
     * 
     */
    uint8_t (*rx_finish)(struct can_dev_t *dev);
    int (*tx_msg_func)(struct can_dev_t *dev,uint8_t *buf,uint8_t len);
    int (*rx_msg_func)(struct can_dev_t *dev);
    int (*set_ide)(struct can_dev_t *dev,uint8_t val);
    int (*set_rtr)(struct can_dev_t *dev,uint8_t val);
    int (*set_dst_id)(struct can_dev_t *dev,uint32_t val);
    int (*set_src_id)(struct can_dev_t *dev,uint32_t val);
    uint8_t (*check_running)(struct can_dev_t *dev);
    void (*can_clear_running_callback)(struct can_dev_t *dev);
	void (*probe_master)(struct can_dev_t *dev);
    void     (*set_limit_flag)(struct can_dev_t *dev ,uint8_t bit_num,uint8_t val);
    void     (*clear_limit_flag)(struct can_dev_t *dev);
    uint8_t  (*get_limit_flag)(struct can_dev_t *dev);
};
struct can_dev_t * can_get_instance(void);
int can_init(struct can_dev_t **dev);
#endif // !_CAN_H_
